<route lang="jsonc" type="page">
    {
      "layout": "tabbar",
      "style": {
			"navigationStyle": "custom",
			"navigationBarTitleText": "我的"
		}
    }
</route>

<script lang="ts" setup>
	import UserInfo from './user.vue'
	import Integral from './integral'
	import OrderMenu from './order-menu.vue'
	import FabServices from '@/pages/components/services.vue'
	import { useUserStore } from '@/store/user'
	import { userInvite } from '@/api/user'
	import IntiteDialog from '@/components/invite-dialog.vue'
	import { onShareAppMessage, onLoad } from '@dcloudio/uni-app'
	import ShareImg from '@img/user_bg.jpg'
	
	const userStore = useUserStore()
	const share_id = ref<any>('')
	const showBreak = ref<boolean>(false)
	
	onLoad((opts: any) => {
		share_id.value = opts.share_id
		console.log('onLoad-me', opts)
		onShareAppMessage(() => {
			return {
				title: '好友邀请-Happy雪具养护中心',
				path: `/pages/me/me?share_id=${userStore.userInfo.id}`,
				imageUrl: ShareImg,
			}
		})
		inviteShareUser()
	})
	
	watch(() => userStore.userInfo.token, (val) => {
		if (val) inviteShareUser()
	})
	
	onMounted(() => {
		// wx.login({
		//   success (res) {
		//     if (res.code) {
		//       //发起网络请求
		//       wx.request({
		//         url: 'https://api.weixin.qq.com/sns/jscode2session',
		//         data: {
		//         appid: 'wx2fa45dc6458b586c',
		//         secret: '1589a5cb462774935b45653a07c497da',
		//         js_code: res.code,
		//         grant_type: 'authorization_code'
		//         },
		//         success: res => {
		//         const openid = res.data.openid; // 用户的openid
		//         const sessionKey = res.data.session_key; // 会话密钥
		//         console.log(openid, sessionKey);
		//       }
		//       })
		//     } else {
		//       console.log('登录失败！' + res.errMsg)
		//     }
		//   }
		// })
	})
	// 加入关系
	function inviteShareUser() {
		const token = uni.getStorageSync('token')
		const id = share_id.value || uni.getStorageSync('share_id')

		// 存在分享者id
		if (id) {
			// 未登录，存储分享者id
			if (!token) {
				uni.setStorageSync('share_id', id)
				return
			}
		
			if (id == userStore.userInfo.id) return
			
			senduserInvite()
		}
	}
	// 加入关系接口请求
	function senduserInvite() {
		const id = share_id.value || uni.getStorageSync('share_id')

		userInvite({ invite_id: id }).then(res => {
			console.log(res, res)
			if (res?.code === 0) {
				// 存在加入的关系，提示脱离关系在加入
				if (res.data == 1) {
					showBreak.value = true
				}
			} else {
				handleCancel()
				uni.showToast({
					icon: 'success',
					title: '加入关系成功'
				})
			}
		})
	}

	// 跳转我的雪具
	function handleSnowboard() {
		uni.navigateTo({
			url: `/pages-sub/snowboard/index`
		})
	}
	// 关系脱离成功回调
	// function handleUnlink() {
	// 	senduserInvite()
	// }
	// 分享者id清空
	function handleCancel() {
		// 处理分享者id
		uni.setStorageSync('share_id', '')
		share_id.value = ''
	}
</script>

<template>
	<view class="me-page">
		<!-- 用户信息 -->
		<user-info />

		<view v-if="userStore.userInfo.token" class="content-view">
			<!-- 积分 -->
			<integral />
			<!-- 订单模块 -->
			<order-menu />
			<!-- 雪具管理 -->
			<view class="card-item item-snowboard">
				<view class="cell-item shadow">
					<view class="cell-item_center">雪具管理</view>
					
					<view class="cell-item_right" @click="handleSnowboard">
						<text>雪具详情</text>
						<wd-icon name="arrow-right" size="18px" color='#BDBCBC'></wd-icon>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 客服按钮 -->
		<FabServices bottom="17" />
		<!-- 脱离关系弹窗 -->
		<intite-dialog v-model="showBreak" content="您确定离开现在的网络关系，加入新的网络关系？" :toast='false' @success="senduserInvite" @cancel="handleCancel" />
	</view>
</template>

<style lang="scss" scoped>
	.me-page {
		.content-view {
			padding: 50px 26px 64px 26px;
			
			.item-snowboard {
				margin-top: 24px;
			}
		}
	}
</style>
